﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Work.ClientEngine.FPXH
{
    internal class AddressDformat : CommandFormat
    {
        public AddressDformat(string addresscode) : base(addresscode)
        {
        }

        public override EventArgs AnalysisReadPacket(string cmd, int count, int slaveID = 1)
        {
            ReadDataEventArgs args = new ReadDataEventArgs()
            {
                DebugMsg = string.Empty,
                Value = new int[count]
            };
            if (cmd.Length > 9)
            {
                //读取到数据
                int length = 9 + 4 * count;
                if (cmd.Length != length)
                    args.DebugMsg = "数据丢包";
                string ds = cmd.Substring(6, 4 * count);
                for (int i = 0; i < count; i++)
                {
                    string d = ds.Substring(i * 4, 4);
                    d = string.Format("{0}{1}{2}{3}", d[2], d[3], d[0], d[1]);
                    args.Value[i] = Convert.ToInt32(d, 16);
                }
            }
            else if (cmd.Length == 9)
            {
                args.DebugMsg = cmd.Substring(4, 2);
            }
            else
            {
                args.DebugMsg = "数据丢包";
            }
            return args;
        }

        public override EventArgs AnalysisWritePacket(string cmd, int slaveID = 1)
        {
            WriteDataEventArgs args = new WriteDataEventArgs()
            {
                WriteOK = true,
                DebugMsg = string.Empty
            };
            if (cmd.Length == 9 && cmd.StartsWith("%01$WD"))
            {

            }
            else if (cmd.Length == 9)
            {
                args.WriteOK = false;
                args.DebugMsg = cmd.Substring(4, 2);
            }
            else
            {
                args.WriteOK = false;
                args.DebugMsg = "命令数据丢包";
            }

            return args;
        }

        public override byte[] MakeReadPacket(string[] address, int[] values, int slaveID = 1)
        {
            string cmd = string.Format("%{0}#RDD{1}{2}", slaveID.ToString("00"), address[0].Substring(1), address[1].Substring(1));
            cmd = string.Format("{0}{1}{2}", cmd, GetBCC(cmd),"\r");            
            return Encoding.ASCII.GetBytes(cmd);
        }

        public override byte[] MakeWritePacket(string[] address, int[] values, int slaveID = 1)
        {
            string cmd = string.Format("%{0}#WDD{1}{2}", slaveID.ToString("00"), address[0].Substring(1), address[1].Substring(1));
            //添加数据
            int count = values.Length;
            for (int i = 0; i < count; i++)
            {
                string s= Convert.ToString(values[i], 16).PadLeft(4, '0').ToUpper();
                s = string.Format("{0}{1}{2}{3}", s[2], s[3], s[0], s[1]);
                cmd += s;
            }
            cmd = string.Format("{0}{1}{2}", cmd, GetBCC(cmd), "\r");
            return Encoding.ASCII.GetBytes(cmd);
        }
    }
}
